package com.chen10.gobang.controller;

import com.chen10.gobang.common.enums.response.SuccessMsgEnum;
import com.chen10.gobang.common.exception.BusinessException;
import com.chen10.gobang.common.protocal.ResponseResult;
import com.chen10.gobang.common.protocal.ResponseVO;
import com.chen10.gobang.model.dto.RemoveFriendDTO;
import com.chen10.gobang.model.vo.FriendVO;
import com.chen10.gobang.service.FriendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/friend")
@Slf4j
public class FriendController {
    @Resource
    private FriendService friendService;

    /**
     * 获取好友列表
     * 获取指定用户id的好友列表
     * @param userId 指定用户的用户id
     * @return 包含好友信息、在线状态的好友列表
     */
    @GetMapping("/list/{userId}")
    public ResponseVO<List<FriendVO>> getFriendList(@PathVariable Integer userId) {
        log.info("获取好友列表,userId:{}", userId);
        List<FriendVO> friendList = null;
        try {
            friendList = friendService.getFriendList(userId);
            return ResponseResult.success(SuccessMsgEnum.GET_FRIEND_LIST_SUCCESS.getMsg(),friendList);
        } catch (BusinessException e) {
            return ResponseResult.error(e.getCode(), e.getMessage());
        }
    }


    /**
     * 删除好友
     *
     * @param removeFriendDTO
     * @return
     */
    @PostMapping("/remove")
    public ResponseVO removeFriend(@RequestBody RemoveFriendDTO removeFriendDTO) {
        log.info("移除好友,removeFriendDTO:{}", removeFriendDTO);
        try {
            friendService.removeFriend(removeFriendDTO);
            return ResponseResult.success(SuccessMsgEnum.FRIEND_DELETE_SUCCESS);
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(),e.getMessage());
        }
    }
}
